<?php

class Soul_Template_Context_Method {

    public function __construct() {
        
    }

    public function gridHelper($tmpl, $id = NULL, array $datasource = array()) {
        $result = new Soul_Template_Html_Grid();
        return $result->setId($id)->setDataSource($datasource);
    }

    public function formHelper($tmpl, $id = NULL, $action = NULL, $method = 'POST') {
        $result = new Soul_Template_Html_Form();
        return $result->setId($id)->setAction($action)->setMethod($method);
    }

    public function textboxHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Textbox();
        return $result->setId($id)->setName($name);
    }

    public function textareaHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Textarea();
        return $result->setId($id)->setName($name);
    }

    public function selectHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Select();
        return $result->setId($id)->setName($name);
    }

    public function treeSelectHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Select_Tree();
        return $result->setId($id)->setName($name);
    }

    public function passwordHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Password();
        return $result->setId($id)->setName($name);
    }

    public function checkboxHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Checkbox();
        return $result->setId($id)->setName($name);
    }

    public function emailHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Email();
        return $result->setId($id)->setName($name);
    }

    public function fileHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_File();
        return $result->setId($id)->setName($name);
    }

    public function datetimeHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Datetime();
        return $result->setId($id)->setName($name);
    }

    public function hiddenHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Hidden();
        return $result->setId($id)->setName($name);
    }

    public function submitHelper($tmpl, $text, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Button_Submit();
        return $result->setText($text)->setId($id);
    }

    public function resetHelper($tmpl, $text, $id = NULL) {
        $result = new Soul_Template_Html_Button_Reset();
        return $result->setId($id)->setName($name);
    }

    public function buttonHelper($tmpl, $text, $click, $id = NULL) {
        $result = new Soul_Template_Html_Button_Button();
        return $result->setText($text)->click($click)->setId($id);
    }

    public function linkButtonHelper($tmpl, $text, $click, $id = NULL) {
        $result = new Soul_Template_Html_Button_Link();
        return $result->setText($text)->click($click)->setId($id);
    }

    public function linkHelper($tmpl, $text, $url, $target = NULL) {
        $result = '<a class="link" href="' . $url . '"';
        if ($target) {
            $result .= ' target="' . $target . '"';
        }
        $result .= '><span><span>' . $text . '</span></span></a>';
        return $result;
    }

    public function booleanHelper($tmpl, $id = NULL, $name = NULL) {
        $result = new Soul_Template_Html_Form_Boolean();
        return $result->setId($id)->setName($name);
    }

    public function toolbarHelper($tmpl, $id = NULL) {
        $result = new Soul_Template_Html_Toolbar();
        return $result->setId($id);
    }

    public function paginationHelper($tmpl, $records, $url, $pagesize = 20) {
        $result = new Soul_Template_Html_Pagination();
        return $result->setRecordCount($records)->setPageSize(20)->setUrlFormat($url);
    }

    public function renderReferenceScriptHelper($tmpl) {
        $result = '';
        $res = Soul_Template_Resource::getInstance();
        foreach ($res->getReferenceScripts() as $script) {
            $result .= '<script language="javascript" src="' . $script . '"></script>' . "\n";
        }
        return $result;
    }

    public function renderStartupScriptHelper($tmpl) {
        $res = Soul_Template_Resource::getInstance();
        if (count($res->getStartupScripts()) > 0) {
            return '<script language="javascript">jQuery(function(){' . implode("\n", $res->getStartupScripts()) . "});</script>\n";
        }
        return '';
    }

    public function renderScriptBlockHelper($tmpl) {
        $res = Soul_Template_Resource::getInstance();
        if (count($res->getScriptBlocks()) > 0) {
            return '<script language="javascript">' . implode("\n", $res->getScriptBlocks()) . "</script>\n";
        }
        return '';
    }

    public function renderReferenceCssHelper($tmpl) {
        $result = '';
        $res = Soul_Template_Resource::getInstance();
        foreach ($res->getReferenceCss() as $css) {
            $result .= '<link rel="stylesheet" type="text/css" href="' . $css . '">' . "\n";
        }
        return $result;
    }

    public function renderCssBlockHelper($tmpl) {
        $res = Soul_Template_Resource::getInstance();
        if (count($res->getScriptBlocks()) > 0) {
            return '<style>' . implode("\n", $res->getScriptBlocks()) . "</style>\n";
        }
        return '';
    }

    public function chooseHelper($tmpl, $value, $default = '') {
        return empty($value) ? $default : $value;
    }

}